package cmw.com.beijingnew.activity;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;


import cmw.com.beijingnew.R;
import cmw.com.beijingnew.fragment.ContentFragment;
import cmw.com.beijingnew.fragment.LeftmenuFragment;
import cmw.com.utils.DensityUtil;
import cmw.com.utils.UiUtil;

public class MainActivity extends AppCompatActivity {

    public static final String FM_MAIN = "fm_main";
    public static final String FM_LEFTMENU = "fm_leftmenu";
    public  DrawerLayout mDrawerLayout = null;
    public LinearLayout leftDrawer = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initInstance();
        initFragment();
    }

    /**
     * 菜单初始化
     */
    private void initInstance() {


    /*1.设置主页面*/
        setContentView(R.layout.activity_main);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        leftDrawer = (LinearLayout) findViewById(R.id.left_drawer);
        //设置 DrawLayout 可全屏滑动 (默认DrawLayout 只能从侧边滑动)
        UiUtil.setDrawerLeftEdgeSize(this, mDrawerLayout, 0);
    }

    /**
     * 初始化创建Fragment
     */
    private void initFragment(){
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fm_main, new ContentFragment(), FM_MAIN);
        ft.replace(R.id.left_drawer, new LeftmenuFragment(), FM_LEFTMENU);
        ft.commit();
    }

    public DrawerLayout getDrawerLayout(){
        return mDrawerLayout;
    }

    /**
     *
     * @return
     */
    public LeftmenuFragment getLeftmenuFragment(){
        return (LeftmenuFragment) getSupportFragmentManager().findFragmentByTag(FM_LEFTMENU);
    }

    public void setDrawerLockMode(int lockMode){
        mDrawerLayout.setDrawerLockMode(lockMode);
    }

    public void toggle() {
       int lockMode = mDrawerLayout.getDrawerLockMode(leftDrawer);
        if(lockMode == DrawerLayout.LOCK_MODE_LOCKED_CLOSED){
            mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }
       boolean isOpen = mDrawerLayout.isDrawerOpen(leftDrawer);
       if(isOpen){
           mDrawerLayout.closeDrawer(leftDrawer);
       }else{
           mDrawerLayout.openDrawer(leftDrawer);
       }
    }

    public ContentFragment getContentFragment() {
        return  (ContentFragment) getSupportFragmentManager().findFragmentByTag(FM_MAIN);
    }
}
